package com.example.model.title.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.annotations.*;
import com.example.model.TreeModel;
import com.example.model.title.model.ChannelModel;
import com.example.model.title.model.dto.ChannelModelDto;
import com.example.model.title.service.ChannelModelService;
import com.example.utils.Results;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @Classname ChannelController
 * @Description TODO
 * @Date 2022/8/7 1:17
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@RestController("/channel")
public class ChannelController {

    @Autowired
    ChannelModelService channelModelService;

    @PostMapping("/childlist")
    public JSONObject childListController(){
        List<ChannelModel> channelModels=channelModelService.channelModelList();
        List<ChannelModelDto> channelModelDtoList=new ArrayList<>();
        ChannelModelDto channelModelDtoPid=new ChannelModelDto();
        channelModelDtoPid.setId(0);
        channelModelDtoPid.setPid(-1);
        channelModelDtoPid.setTitle("主栏目");
        channelModelDtoList.add(channelModelDtoPid);
        if(channelModels==null || channelModels.size() == 0){
            return Results.success(channelModelDtoList);
        }
        for (int i = 0; i <channelModels.size() ; i++) {
            ChannelModel channelModel = channelModels.get(i);
            ChannelModelDto channelModelDto=new ChannelModelDto();
            channelModelDto.setId(channelModel.getId());
            channelModelDto.setPid(channelModel.getPid());
            channelModelDto.setTitle(channelModel.getChannelName());
            channelModelDtoList.add(channelModelDto);
        }
        return Results.success(channelModelDtoList);
    }

    @PostMapping("/childmainlist")
    public JSONObject childMainlistController(){
        List<ChannelModel> childMainlist=channelModelService.channelModelList();
        return Results.success(childMainlist);
    }

    @PostMapping(value = "/save",desc = "新增")
    public JSONObject saveController(@RequestBody ChannelModel channelModel){
        if(channelModel==null){
            return Results.error("必要信息不得为空");
        }
        channelModel.setCreatedate(new Date());
        channelModelService.save(channelModel);
        return Results.success("新增成功");
    }

    @PostMapping(value = "/update",desc = "更新")
    public JSONObject updateController(@RequestBody ChannelModel channelModel){
        channelModelService.update(channelModel);
        return Results.success("更新成功");
    }

    @GetMapping(value = "/getone",desc = "单独获取一个")
    public JSONObject getOneController(@Param("id")String id){
        ChannelModel channelModel = channelModelService.getId(id);
        return Results.success(channelModel);
    }


}
